package com.kuangstudy.common.demo6;

import java.math.BigDecimal;

/**
 * ClassName: Demo7
 * date: 2021/07/14 17:20:06
 *浮点数最好别用来计算
 * @author zlk
 */
public class Demo7 {
    public static void main(String[] args) {
        double d1 = 1.0;
        double d2 = 0.9;
        System.out.println(d1-d2);//0.09999999999999998

        double result = (1.4-0.5)/0.9;
        System.out.println(result);//0.9999999999999999
        /*BigDecimal,大的浮点数精确计算*/
        BigDecimal bd1 = new BigDecimal("1.0");
        BigDecimal bd2 = new BigDecimal("0.9");
        /*subtract减法计算*/
        BigDecimal subtract = bd1.subtract(bd2);
        System.out.println(subtract);
        /*add加法*/
        BigDecimal add = bd1.add(bd2);
        System.out.println(add);
        /*multiply乘法*/
        BigDecimal multiply = bd1.multiply(bd2);
        System.out.println(multiply);
        /*除法 divide() scale表示保留几位小数 BigDecimal.ROUND_HALF_UP表示四舍五入*/
        BigDecimal divide = new BigDecimal("20").divide(new BigDecimal("3"),2,BigDecimal.ROUND_HALF_UP);
        System.out.println(divide);//6.67
    }
}
